package treasurer;

import home.HomePage;

import java.io.IOException;
import java.io.Reader;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;

import president.ViewForm;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import controllers.Accountability;
import controllers.BKPayroll;
import controllers.Registered;

import secretary.AddStudentAccount;
import treasurer.ViewFirst.CustomCheckBox;
import treasurer.ViewFirst.CustomCheckBoxModel;

public class ClearancePageFourth extends WebPage{
	
	public ClearancePageFourth() throws IOException, SQLException
	{
		Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
		final SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
		
		add(new BookmarkablePageLink("home", TreasurerHome.class));
		add(new BookmarkablePageLink("logoutPage", HomePage.class));
		add(new BookmarkablePageLink("view", AccountabilityItemList.class));
		add(new BookmarkablePageLink("first", ClearancePageFirst.class));
		add(new BookmarkablePageLink("second", ClearancePageSecond.class));
		add(new BookmarkablePageLink("third", ClearancePageThird.class));
		add(new BookmarkablePageLink("fourth", ClearancePageFourth.class));
			
		
		Form form = new Form("AccountabilityListForm");
		
		form.add(new Button("view"){
			private static final long serialVersionUID = 1L;
			
			public void onSubmit(){
				try{
					if(selections.size()!=1){
						error("Please choose one account.");
					}
					else{
						String formID = selections.get(0);
											
		    			ViewFirst page = new ViewFirst(formID);
		    			setResponsePage(page);
					}
	    		}catch (IOException e){
	    			e.printStackTrace();
	    		}catch (SQLException e){
	    			e.printStackTrace();
	    		}
			}
		});
		
		List<Registered> first = (List<Registered>)sqlMap.queryForList("Registered.getRegisteredByYear", 4);
		
		List studentids = new ArrayList();
		Registered one = null;
		
	    for (Registered b : first)
	    {
	    	String id = b.getStudent_id();
	    	List<Accountability> acc = (List<Accountability>)sqlMap.queryForList("Accountability.isAccountable", id);
	    	
	    	if(acc.size() == 0)
	    	{
	    		final List ids = new ArrayList();
    			ids.add(b.getStudent_id());
	    	}
	    	else
	    	{
	    		studentids.add(b.getStudent_id());
	    	}
	    }
		
	    ListView Year = new ListView("Year", studentids) {
	        protected void populateItem(ListItem item) {
	        	String id = (String) item.getModelObject();
	        	item.add(new CustomCheckBox("checkbox", new CustomCheckBoxModel(id)));
	            item.add(new Label("id", item.getModel()));
	        }
	    };
		
	    form.add(Year);
	    add(form);
	}
	
	public class CustomCheckBox extends CheckBox{
		private static final long serialVersionUID = 1L;

		public CustomCheckBox(String id, IModel model) {
			super(id, model);
		}
		
		protected boolean wantOnSelectionChangedNotifications() {
			return true;
		}
	}

	private final List<String> selections = new ArrayList();
	
	private class CustomCheckBoxModel implements IModel, Serializable{
		private static final long serialVersionUID = 1L;
		private final String id;
		
		public CustomCheckBoxModel(String id2) {
			super();
			this.id = id2;
		}

		public IModel getNestedModel() {
			return null;
		}

		public Object getObject(){
			return isSelected();
		}
		
		private Boolean isSelected() {
			if (selections.contains(id))
				return true;
			else
				return false;
		}
			
		public void setObject(Object object){
			boolean selected = ((Boolean) object).booleanValue();
			boolean previouslySelected = isSelected();
			if (selected){
				if (!previouslySelected) {
					selections.add(id);
				}
			} else {
				if (previouslySelected) {
					selections.remove((String)id);
				}
			}
		}
		public void detach() {}
	}
}
		
